package io.zerax.moyu_chat;

import android.app.DialogFragment;
import android.os.Bundle;
import android.app.Dialog;
import android.app.AlertDialog;
import java.util.List;
import java.util.ArrayList;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

public class ConnectDialogFragment extends DialogFragment
{
    public static interface OnValueInput {
        void input(String msg, boolean isRoom);
    }
    
    private String title;
    private List<OnValueInput> onValueInputListeners;
    
    public ConnectDialogFragment() {
        onValueInputListeners = new ArrayList<>();
    }
    
    public void addOnValueInputListener(OnValueInput lisener) {
        onValueInputListeners.add(lisener);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
		title = getString(R.string.connect);
		
		View dialogView = getActivity().getLayoutInflater().inflate(R.layout.connect, null);
		
		final EditText addrInput = dialogView.findViewById(R.id.connect_addr);
		final RadioGroup connectMethod = dialogView.findViewById(R.id.connect_select_type);
		
		
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setView(dialogView);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface i, int id) {
				String input = addrInput.getEditableText().toString();
				for (OnValueInput listener: onValueInputListeners) {
					listener.input(input, connectMethod.getCheckedRadioButtonId() == R.id.connect_type_room);
				}
				
				addrInput.getEditableText().clear();
			}
		});
        return builder.create();
    }
}
